//
//  YMSLoginView.swift
//  YMSWeibo
//
//  Created by 杨茂盛 on 2016/11/13.
//  Copyright © 2016年 杨茂盛. All rights reserved.
//

import UIKit
import SnapKit

//-----------------------------------创建协议-------------------------------------------
@objc protocol YMSLoginViewProtocol:NSObjectProtocol {
    //在swift中使用协议 遵守的是NSObjectProtocol
    //在swift中方法是默认实现的 不实现会报错的

    //创建按钮登录方法
    @objc optional func loginBtn()
    
    //创建按钮注册方法
       func resgistBtn()
}
//---------------------------------------------------------------------------------------

class YMSLoginView: UIView {
    
    //MRAK:设置代理 代理使用的修饰符是weak
    weak var delegate:YMSLoginViewProtocol?
    
  @objc private func loginBtnDidClick(){
        delegate?.loginBtn?()
    }
    @objc private func registerBtnDidClick(){
        delegate?.resgistBtn()
    }

    //外界设置信息的接口
    func setInFo(title:String,imageName:String,isHome:Bool = false){
        //修改文字
        tipLabel.text = title
        //修改圆圈的图片
        cicleView.image = UIImage(named: imageName)
        //判断是否执行动画
        if  isHome {
            //执行动画
            self.startAnimation()
        }else{
        //不是主页就隐藏该不该显示的控件
            self.largeHouse.isHidden = true
            self.backView.isHidden = true
        }
    }
    //MRAK:执行动画
  private  func startAnimation(){
        let annimation = CABasicAnimation(keyPath: "transform.rotation")
        //设置动画的一些属性
        //动画的持续时间
        annimation.repeatCount = MAXFLOAT
        //动画的执行速度
        annimation.duration = 20
        //动画旋转的角度
        annimation.toValue = M_PI*2
        //动画完成之后不从视图移除
        annimation.isRemovedOnCompletion = false

        //添加动画
        self.cicleView.layer .add(annimation, forKey: nil)
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        //搭建界面
        setUI()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //MARK:搭建主界面
    func setUI(){
        //监听按钮的点击事件
        myLoginBtn.addTarget(self, action: #selector(loginBtnDidClick), for: .touchUpInside)
        myRegisBtn.addTarget(self, action: #selector(registerBtnDidClick), for: .touchUpInside)

        //添加到根视图
        addSubview(cicleView)
        addSubview(backView)
        addSubview(largeHouse)
        addSubview(tipLabel)
        addSubview(myLoginBtn)
        addSubview(myRegisBtn)
        //开始布局
        //圆
        cicleView.snp.makeConstraints { (make) in
            make.centerX.equalTo(self.snp.centerX)
            make.centerY.equalTo(self.snp.centerY).offset(-80)
        }
        //房子
        largeHouse.snp.makeConstraints { (make) in
            make.centerX.equalTo(cicleView)
            make.centerY.equalTo(cicleView)
        }
        //提交方案文字
        tipLabel.snp.makeConstraints { (make) in
            make.top.equalTo(cicleView.snp.bottom).offset(20)
            make.centerX.equalTo(cicleView)
        }
        //登录按钮
        myLoginBtn.snp.makeConstraints { (make) in
            make.left.equalTo(tipLabel)
            make.top.equalTo(tipLabel.snp.bottom).offset(20)
            make.width.equalTo(100)
        }
        //注册
        myRegisBtn.snp.makeConstraints { (make) in
            make.right.equalTo(tipLabel)
            make.top.equalTo(tipLabel.snp.bottom).offset(20)
            make.width.equalTo(100)
        }
        //背景遮罩
        backView.snp.makeConstraints { (make) in
            make.center.equalTo(cicleView)
        }
        //设置背景颜色
        self.backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1)
    }
    //MARK:实例化子控件
    //圆上面的遮罩
    private lazy var backView:UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_mask_smallicon"))
    //圈圈
    private lazy var cicleView:UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_smallicon"))
    ///MARK:房子
    private lazy var largeHouse:UIImageView = UIImageView(image: #imageLiteral(resourceName: "visitordiscover_feed_image_house"))
    //MARK:提交文案
    private lazy var tipLabel:UILabel = {
        let myLabel = UILabel()
        myLabel.text = "关注一些人,关注一些人,回到这里看看有什么惊喜"
        myLabel.numberOfLines = 0
        myLabel.textAlignment = .center
        myLabel.textColor = #colorLiteral(red: 0.8549019694, green: 0.250980407, blue: 0.4784313738, alpha: 1)
        myLabel.font = UIFont.systemFont(ofSize: 14)
        myLabel.preferredMaxLayoutWidth = 220
        //MARK:关于lable文字换行的高度问题
        //设置这句话文本自动换行的时候,高度比较精确
        //如果设置约束label的左右或者宽度都会造成高度不准确
        return myLabel
    }()
    //MARK:登录按钮
     lazy var myLoginBtn:UIButton = {
        let loginBtn = UIButton()
        loginBtn.setTitle("登录", for: .normal)
         loginBtn.setTitleColor(#colorLiteral(red: 0.2588235438, green: 0.7568627596, blue: 0.9686274529, alpha: 1), for: .normal)
        loginBtn .setBackgroundImage(#imageLiteral(resourceName: "common_button_white"), for: .normal)
        loginBtn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        return loginBtn
    }()
    //MARK:注册按钮
    lazy var myRegisBtn:UIButton = {
        let regisBtn = UIButton()
        regisBtn.setTitle("注册", for: .normal)
        regisBtn.setTitleColor(#colorLiteral(red: 0.9411764741, green: 0.4980392158, blue: 0.3529411852, alpha: 1), for: .normal)
        regisBtn .setBackgroundImage(#imageLiteral(resourceName: "common_button_white"), for: .normal)
        regisBtn.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        return regisBtn
    }()
    
}
